package com.xszx.dao.impl;

import com.xszx.bean.Goods;
import com.xszx.bean.dto.CartDto;
import com.xszx.dao.CartDao;
import com.xszx.dao.GoodsDao;
import com.xszx.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    static GoodsDao goodsDao = new GoodsDaoImpl();

    @Override
    public List<CartDto> getCartsByUserId(Integer userId) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_cart where user_id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, userId);
            ResultSet resultSet = ps.executeQuery();

            List<CartDto> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                int goodsId = resultSet.getInt("goods_id");
                //根据goodsId查询商品信息
                Goods goods = goodsDao.getGoodsById(goodsId);
                int goodsCount = resultSet.getInt("goods_count");
                CartDto cartDto = new CartDto(id, userId, goodsId, goodsCount, goods.getGoodsName(), goods.getGoodsPrice(), goods.getGoodsImageUrl(), goods.getGoodsState());
                list.add(cartDto);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void deleteCart(Integer id) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "delete from t_cart where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, id);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteCartByUserId(Integer userId) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "delete from t_cart where user_id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, userId);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateCartMount(Integer id, Integer goodsCount) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "update t_cart set goods_count = ? where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, goodsCount);
            ps.setInt(2, id);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<CartDto> getCartDtoByIds(List<Integer> ids) {
        GoodsDao goodsDao = new GoodsDaoImpl();
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_cart where id in (";
            for (Integer id : ids) {
                sql += id + ",";
            }
            sql = sql.substring(0, sql.length() - 1) + ")";
            PreparedStatement ps = connection.prepareStatement(sql);

            ResultSet resultSet = ps.executeQuery();
            List<CartDto> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                int userId = resultSet.getInt("user_id");
                int goodsId = resultSet.getInt("goods_id");
                Goods goods = goodsDao.getGoodsById(goodsId);
                int goodsCount = resultSet.getInt("goods_count");
                CartDto cartDto = new CartDto(id, userId, goodsId, goodsCount, goods.getGoodsName(), goods.getGoodsPrice(), goods.getGoodsImageUrl(), goods.getGoodsState());
                list.add(cartDto);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void addCart(Integer userId, Integer goodsId, Integer goodsCount) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "insert into t_cart values(null, ?, ?, ?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, userId);
            ps.setInt(2, goodsId);
            ps.setInt(3, goodsCount);

            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
